<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    cancelText="关闭"
  >
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk" :loading="buttonok">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                @change="autoValue"
                :disabled="disableSubmit"
                v-decorator="['partyId', validatorRules.partyId]"
                dict="Person,name,partyId,typeId = EMPLOYEE"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动前部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select
                :disabled="true"
                v-decorator="['beforeDepId', validatorRules.beforeDepId]"
                pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                placeholder="请选择调动前部门"
                back="name"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动前职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="true"
                v-decorator="['beforeJobId', validatorRules.beforeJobId]"
                placeholder="请输入调动前职位"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动后部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select
                :disabled="disableSubmit"
                v-decorator="['afterDepId', validatorRules.afterDepId]"
                pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                placeholder="请选择调动后部门"
                back="name"
                @change="handleCategoryChange"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动后职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['afterJobId', validatorRules.afterJobId]"
                placeholder="请输入调动后职位"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择调动日期"
                v-decorator="['moveDate', validatorRules.moveDate]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="调动原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['description', validatorRules.description]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = TRANSFER_REASON"
                placeholder="请选择调动原因"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
          <a-form-item v-show="false">
            <a-input :disabled="disableSubmit" v-decorator="['name']"></a-input>
          </a-form-item>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JCategorySelect from '@/components/jeecg/JCategorySelect'

export default {
  name: 'HrEmployeeMoveModal',
  components: {
    JDate,
    JSearchSelectTag,
    JCategorySelect,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      buttonok: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      disableSubmit: false,
      validatorRules: {
        partyId: {
          rules: [{ required: true, message: '请输入员工!' }],
        },
        beforeDepId: {
          rules: [{ required: true, message: '请输入调动前部门!' }],
        },
        beforeJobId: {
          rules: [{ required: true, message: '请输入调动前职位!' }],
        },
        afterDepId: {
          rules: [{ required: true, message: '请输入调动后部门!' }],
        },
        afterJobId: {
          rules: [{ required: true, message: '请输入调动后职位!' }],
        },
        description: {
          rules: [{ required: true, message: '请输入调动原因!' }],
        },
        moveDate: {
          rules: [{ required: true, message: '请输入调动日期!' }],
        },
      },
      url: {
        add: '/online/api/createHrEmployeeMove',
        edit: '/online/api/updateHrEmployeeMove',
        get: '/online/api/getHrEmployeeMove',
        getHrEmployee: '/online/api/getHrEmployee',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        httpAction(this.url.get, { id: record.id }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'partyId',
                  'beforeDepId',
                  'beforeJobId',
                  'afterDepId',
                  'afterJobId',
                  'description',
                  'moveDate',
                  'remark'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          that.buttonok = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success('提交成功!')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
              setTimeout(() => {
                that.buttonok = false
              }, 500)
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'partyId', 'afterDepId', 'afterJobId', 'description', 'moveDate','remark'))
    },
    handleCategoryChange(value, backObj) {
      this.form.setFieldsValue(backObj)
    },
    autoValue(e) {
      httpAction(this.url.getHrEmployee, { partyId: e }, 'post').then((res) => {
        let model = Object.assign({}, res.result)
        model.beforeDepId = res.result.departmentId
        model.beforeJobId = res.result.positionName
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(model, 'beforeDepId', 'beforeJobId'))
        })
      })
    },
  },
}
</script>
